# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _


import logging
_logger = logging.getLogger(__name__)

class AccountJournal(models.Model):
    _inherit = "account.journal"
    
    type = fields.Selection(selection_add = [
            ('profit', 'Profit'),
            ('tax', 'Tax'),
            ('stock', 'Stock'),
        ])
    

class AccountChartTemplate(models.Model):
    _inherit = "account.chart.template"

    @api.multi
    def _prepare_profit_journals(self, acc_template_ref, company, journals_dict=None):
        profit_account = acc_template_ref.get(self.property_account_profit_id.id)        
        res = []
        res.append({
            'type': 'profit',
            'internal_code': 'profit',
            'name': _('结转利润账簿'),
            'code': 'PRFT',
            'company_id': company.id,
            'default_credit_account_id': profit_account,
            'default_debit_account_id': profit_account,
            'show_on_dashboard': True,
        })
        return res

    @api.multi
    def _prepare_tax_journals(self, acc_template_ref, company, journals_dict=None):
        vat_income_account = acc_template_ref.get(self.property_account_vat_income_id.id)        
        vat_expense_account = acc_template_ref.get(self.property_account_vat_expense_id.id)        
        
        res = []
        res.append({
            'type': 'tax',
            'internal_code': 'vat',
            'name': _('增值税账簿'),
            'code': 'VAT',
            'company_id': company.id,
            'default_credit_account_id': vat_income_account,
            'default_debit_account_id': vat_expense_account,
            'show_on_dashboard': True,
        })

        tax_expense_account = acc_template_ref.get(self.property_account_tax_expense_id.id)
        
        res.append({
            'type': 'tax',
            'internal_code': 'tax',
            'name': _('附加税账簿'),
            'code': 'TAX',
            'company_id': company.id,
            'default_credit_account_id': tax_expense_account,
            'default_debit_account_id': tax_expense_account,
            'show_on_dashboard': True,
        })

        return res

    @api.multi
    def _prepare_stock_journals(self, acc_template_ref, company, journals_dict=None):

        material_account = acc_template_ref.get(self.property_account_material_id.id)        
        goods_account = acc_template_ref.get(self.property_account_goods_id.id)        
        production_account = acc_template_ref.get(self.property_account_production_id.id)        
        expense_account = acc_template_ref.get(self.property_account_expense_categ_id.id)        
        inventory_material_account = acc_template_ref.get(self.property_account_inventory_material_id.id)        
        inventory_goods_account = acc_template_ref.get(self.property_account_inventory_goods_id.id)        


        res = []

        res.append({
            'type': 'stock',
            'internal_code': 'material',
            'name': _('原材料账簿'),
            'code': 'STK11',
            'company_id': company.id,
            'default_credit_account_id': inventory_material_account,
            'default_debit_account_id': material_account,
            'show_on_dashboard': True,
        })

        res.append({
            'type': 'stock',
            'internal_code': 'goods',
            'name': _('库存商品账簿'),
            'code': 'STK12',
            'company_id': company.id,
            'default_credit_account_id': inventory_goods_account,
            'default_debit_account_id': goods_account,
            'show_on_dashboard': True,
        })

        res.append({
            'type': 'stock',
            'internal_code': 'production',
            'name': _('生产账簿'),
            'code': 'STK13',
            'company_id': company.id,
            'default_credit_account_id': production_account,
            'default_debit_account_id': production_account,
            'show_on_dashboard': True,
        })


        return res


    @api.multi
    def _prepare_all_journals(self, acc_template_ref, company, journals_dict=None):
        """Create the 
        """
        res = super(AccountChartTemplate, self)._prepare_all_journals(
            acc_template_ref, company, journals_dict=journals_dict)
        #if not self == self.env.ref('l10n_cn_smb_2019.l10n_chart_china_small_business'):
        #    return res
        
        res += self._prepare_profit_journals(acc_template_ref, company)
        res += self._prepare_tax_journals(acc_template_ref, company)
        res += self._prepare_stock_journals(acc_template_ref, company)

        
        return res

